package main

import (
	"fmt"
	"github.com/antchfx/htmlquery"
	"github.com/gocolly/colly"
	"io"
	"net/http"
	"os"
	"path/filepath"
	"strings"
	"time"
)

func downloadFile(name, url string) {
	// 创建 img 文件夹
	// 发送 HTTP GET 请求并读取响应
	resp, err := http.Get(url)
	if err != nil {
		fmt.Println(err)
	}
	defer resp.Body.Close()

	// 创建新的 JPG 文件并写入数据
	fileName := name
	filePath := filepath.Join("img", fileName)
	newFile, err := os.Create(filePath)
	if err != nil {
		fmt.Println(err)
	}
	defer newFile.Close()

	_, err = io.Copy(newFile, resp.Body)
	if err != nil {
		fmt.Println(err)
	}

	fmt.Println("File saved to %s", filePath)
}
func main() {
	imgDir := "img"
	if err := os.MkdirAll(imgDir, 0755); err != nil {
		fmt.Println("文件夹已存在")
	}
	c := colly.NewCollector()

	c.OnRequest(func(r *colly.Request) {
		fmt.Println("Visiting", r.URL.String())
	})

	c.Limit(&colly.LimitRule{
		DomainGlob:  "*",
		RandomDelay: 1 * time.Second,
	})

	//收到响应后
	c.OnResponse(func(r *colly.Response) {
		doc, err := htmlquery.Parse(strings.NewReader(string(r.Body)))
		if err != nil {
			fmt.Println(err)
		}
		nodes := htmlquery.Find(doc, `//section/div/figure/a/img`)
		for _, node := range nodes {
			//a := htmlquery.FindOne(node, "./@data-src")
			attr := htmlquery.SelectAttr(node, "data-src")
			attr_name := htmlquery.SelectAttr(node, "title")
			new_name := strings.Replace(attr_name, "图片购买", "", -1)
			fmt.Println(fmt.Sprintf("%s.jpg http:%s", new_name, attr))
			downloadFile(fmt.Sprintf("%s.jpg", new_name), fmt.Sprintf("http:%s", attr))
		}
	})
	c.Visit("https://www.vcg.com/creative-image/2436970")
}
